//
//  UIView+Extension.swift
//  ParkManage
//
//  Created by cf on 2020/9/18.
//  Copyright © 2020 diligrp. All rights reserved.
//

import UIKit

// 坐标相关
extension UIView {
    var x: CGFloat {
        get {
            frame.origin.x
        }
        set {
            frame.origin.x = newValue
        }
    }
    
    var right: CGFloat {
        get {
            return frame.maxX
        }
        set {
            frame.origin.x = newValue - frame.width
        }
    }

    var y: CGFloat {
        get {
            frame.origin.y
        }

        set {
            frame.origin.y = newValue
        }
    }

    var width: CGFloat {
        get {
            frame.width
        }

        set {
            frame.size.width = newValue
        }
    }

    var height: CGFloat {
        get {
            frame.height
        }

        set {
            frame.size.height = newValue
        }
    }

    var size: CGSize {
        get {
            frame.size
        }

        set {
            frame.size = newValue
        }
    }

    var origin: CGPoint {
        get {
            frame.origin
        }

        set {
            frame.origin = newValue
        }
    }
    
    var centerX : CGFloat {
        get {
            return center.x
        }
        set {
            center.x = newValue
        }
    }
    
    var centerY : CGFloat {
        get {
            return center.y
        }
        set {
            center.y = newValue
        }
    }

    var bottom: CGFloat {
        self.frame.origin.y + self.frame.size.height
    }
}

extension UIView {
    
    func makeRoundedCorners(_ radius: CGFloat) {
        layer.cornerRadius = radius
    }

    func makeRoundedCorners() {
        makeRoundedCorners(bounds.size.width / 2)
    }
    
    func roundCorners(_ corners: UIRectCorner, radius: CGFloat) {
        let maskPath = UIBezierPath(
            roundedRect: bounds,
            byRoundingCorners: corners,
            cornerRadii: CGSize(width: radius, height: radius))

        let shape = CAShapeLayer()
        shape.path = maskPath.cgPath
        layer.mask = shape
    }
    
    /**
     设置阴影
    */
    func setShadowWith(bounds: CGRect) {
        
        layer.cornerRadius = 10
        let shadowPath = UIBezierPath(rect: bounds)
        layer.masksToBounds = false
        layer.shadowColor = UIColor.gray.cgColor
        layer.shadowOffset = CGSize(width: 0.0, height: 6.0)
        layer.shadowOpacity = 0.1
        layer.shadowPath = shadowPath.cgPath
    }
    
    /**
            将UIView绘制为虚线分割线
     */
    func transLineToDash() {

        let shapeLayer: CAShapeLayer = CAShapeLayer()
        shapeLayer.bounds = bounds
        shapeLayer.position = CGPoint(x: frame.width / 2, y: frame.height / 2)
        shapeLayer.fillColor = UIColor.clear.cgColor
        shapeLayer.strokeColor = UIColor.lightGray.cgColor
        shapeLayer.lineWidth = 1
        shapeLayer.lineJoin = CAShapeLayerLineJoin.round
        shapeLayer.lineDashPhase = 0
        shapeLayer.lineDashPattern = [NSNumber(value: 4), NSNumber(value: 3)]
        let path: CGMutablePath = CGMutablePath()
        path.move(to: CGPoint(x: 0, y: 1))
        path.addLine(to: CGPoint(x: frame.width, y: 1))
        shapeLayer.path = path
        layer.addSublayer(shapeLayer)
    }
}
